package com.jjb.saas.demo.adapter.controller;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.jjb.saas.demo.app.role.service.RoleService;
import com.jjb.saas.demo.infrastructure.contants.CommonConstant;
import com.jjb.saas.system.client.common.UpdateStatusCmd;
import com.jjb.saas.system.client.role.request.RoleAddCmd;
import com.jjb.saas.system.client.role.request.RolePageQry;
import com.jjb.saas.system.client.role.request.RoleUpdateCmd;
import com.jjb.saas.system.client.role.response.RoleCO;
import com.jjb.saas.system.client.role.response.RoleDetailCO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author tzh * @date 2022年07月08日 18:51
 */
@Api(tags = "【DEMO】角色管理")
@RequestMapping( "${springfox.documentation.swagger-ui.base-url:}/operation/roles")
@RestController
@AllArgsConstructor
public class OperationRoleController {

    private final RoleService roleService;

    @PreAuthorize("@pms.hasPermission('1-6-1')")
    @ApiOperation("新增")
    @PostMapping
    public Response add(@Validated @RequestBody RoleAddCmd cmd){
        return roleService.add(cmd);
    }

    @PreAuthorize("@pms.hasPermission('1-6-2')")
    @ApiOperation("编辑")
    @PutMapping
    public Response update(@Validated @RequestBody RoleUpdateCmd cmd){
        return roleService.update(cmd);
    }

    @CacheEvict(value = CommonConstant.CACHE_PREFFIX + CommonConstant.ROLE,allEntries = true)
    @PreAuthorize("@pms.hasPermission('1-6-3')")
    @ApiOperation("启用禁用")
    @PatchMapping
    public Response updateStatus(@Validated @RequestBody UpdateStatusCmd cmd){
        return roleService.updateStatus(cmd.getId(),cmd.getStatusEnum(),cmd.getVersion());
    }

    @PreAuthorize("@pms.hasPermission('1-6')")
    @ApiOperation("查 询")
    @GetMapping
    public PageResponse<RoleCO> pageRole(RolePageQry qry){
        return roleService.listPage(qry);
    }

    @ApiOperation("详情")
    @GetMapping("/{id}")
    public SingleResponse<RoleDetailCO> getRole(@PathVariable("id") Long id){
        return roleService.getDetail(id);
    }

}
